<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
    <script type="text/javascript">
        (function(){
            Ext.onReady(function(){
                Ext.QuickTips.init();
                var key = '';
                var id = '';

                Ext.define('Department', {
                    extend: 'Ext.data.Model',
                    fields: [
                        {name:'id',type:'string'},
                        {name:'name',type:'string'},
                        {name:'leaderId',type:'string'},
                        {name:'leaderName',type:'string'},
                        {name:'creatorId',type:'string'},
                        {name:'creatorName',type:'string'},
                        {name:'createDate',type:'string'},
                        {name:'version',type:'int'},
                        {name:'status',type:'int'}
                    ]
                });

                var queryName = Ext.create('Ext.form.field.Text',{
                    id:'queryName',
                    name:'queryName'
                });

                var store = Ext.create('Ext.data.Store', {
                    model: 'Department',
                    pageSize:20,
                    proxy: {
                        type:'ajax',
                        url:'/fsxt/sys/department!getDepartment.action',
                        reader: {
                            root: 'root',
                            totalProperty: 'totalProperty'
                        }
                    },
                    autoLoad: true,
                    listeners:{
                        beforeload:function(){
                            store.proxy.extraParams = {
                                name:encodeURIComponent(queryName.getValue())
                            }
                        }
                    }
                });

                var grid = Ext.create('Ext.grid.Panel', {
                    title: '部门管理',
                    store: store,
                    height: '100%',
                    width: '100%',
                    loadMask: true,
                    autoScroll:true,
                    columns:[
                        Ext.create('Ext.grid.RowNumberer'),
                        {text: "部门名称",dataIndex: 'name',sortable: true},
                        {text: "管理者",dataIndex: 'leaderName',sortable: true},
                        {text: "创建者",dataIndex: 'creatorName',sortable: true},
                        {text: "创建时间",dataIndex: 'createDate',sortable: true},
                        {text: "状态",dataIndex: 'status',sortable: true,
                            renderer:function(value){
                                if(value == 1)
                                    return '<font color="blue">启用</font>';
                                return '<font color="red">冻结</font>';
                            }
                        }
                    ],
                    tbar:Ext.create('Ext.toolbar.Toolbar',{
                        items:['-',{
                            xtype:'label',
                            text:'部门名称:'
                        },queryName,'-',{
                            xtype:'button',
                            iconCls:'find',
                            text:'查询',
                            handler:function(){
                                store.loadPage(1);
                            }
                        },'-','->','-',{
                            xtype:'button',
                            iconCls:'create',
                            id:'createButton',
                            text:'新建',
                            handler:function(b,e){
                                key = 'create';
                                window.show(b);
                                Ext.getCmp('name').setValue('');
                                sysOperator.clearValue();
                            }
                        },'-',{
                            xtype:'button',
                            iconCls:'update',
                            text:'修改',
                            id:'updateButton',
                            handler:function(b,e){
                                key = 'update';
                                var selected = grid.getView().getSelectionModel().getSelection();
                                if(selected.length == 0){
                                    Ext.Msg.show({title:'提示',msg:'请选择要修改记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                                    return;
                                }
                                var record = selected[0];
                                if(record.get('status') == 0){
                                    Ext.Msg.show({title:'提示',msg:'记录已冻结,若要使用请先启用',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                                    return;
                                }
                                id = record.get('id');
                                window.show(b);
                                Ext.getCmp('name').setValue(record.get('name'));
                                sysOperator.setValue(record.get('leaderId'));
                            }
                        },'-',{
                            xtype:'button',
                            iconCls:'delete',
                            text:'冻结',
                            handler:function(){
                                var selected = grid.getView().getSelectionModel().getSelection();
                                if(selected.length == 0){
                                    Ext.Msg.show({title:'提示',msg:'请选择要冻结记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                                    return;
                                }
                                var record = selected[0];
                                if(record.get('status') == 0){
                                    Ext.Msg.show({title:'提示',msg:'记录为冻结状态',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                                    return;
                                }
                                Ext.Msg.confirm('提示','确定冻结此部门?',function(btn){
                                    if(btn == 'yes'){
                                        var params = {};
                                        params.id = record.get('id');
                                        var data = {};
                                        data.params = params;
                                        data.url = '/fsxt/sys/department!deleteDepartment.action';
                                        extAjaxSubmit.call(data);
                                    }
                                });
                            }
                        },'-',{
                            xtype:'button',
                            iconCls:'accept',
                            text:'启用',
                            handler:function(){
                                var selected = grid.getView().getSelectionModel().getSelection();
                                if(selected.length == 0){
                                    Ext.Msg.show({title:'提示',msg:'请选择要启用记录',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                                    return;
                                }
                                var record = selected[0];
                                if(record.get('status') == 1){
                                    Ext.Msg.show({title:'提示',msg:'记录为启用状态',icon:Ext.Msg.INFO,buttons:Ext.Msg.OK});
                                    return;
                                }
                                Ext.Msg.confirm('提示','启用此部门?',function(btn){
                                    if(btn == 'yes'){
                                        var params = {};
                                        params.id = record.get('id');
                                        var data = {};
                                        data.params = params;
                                        data.url = '/fsxt/sys/department!enableDepartment.action';
                                        extAjaxSubmit.call(data);
                                    }
                                });
                            }
                        },'-']
                    }),
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: store,
                        displayInfo: true,
                        displayMsg: '当前第 {0} - {1} 条记录,共 {2} 条',
                        emptyMsg: "没有部门记录"
                    }),
                    renderTo:Ext.getBody()
                });

                var sysOperator = Ext.create('Ext.form.ComboBox', {
                    fieldLabel: '管理者',
                    queryMode: 'local',
                    displayField: 'label',
                    valueField: 'value',
                    allowBlank:false,
                    store:Ext.create('Ext.data.Store', {
                        model: 'commonModel',
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sysBase!getSysOperator.action'
                        },
                        autoLoad: true
                    })
                });

                var form = Ext.create('Ext.form.Panel', {
                    items: [{
                        xtype:'textfield',
                        fieldLabel: '部门名称',
                        id:'name',
                        name: 'name',
                        allowBlank: false
                    },sysOperator]
                });

                var window = Ext.create('Ext.window.Window', {
                    title: '部门信息',
                    layout: 'fit',
                    closeAction:'hide',
                    resizable:false,
                    autoHeight:true,
                    width:300,
                    plain: true,
                    modal:true,
                    draggable:true,
                    items:form,
                    buttons:[{
                        text:'保存',
                        handler:function(){
                            var params = {};
                            params.key = key;
                            params.id = id;
                            params.name = encodeURIComponent(Ext.getCmp('name').getValue());
                            params.leaderId = sysOperator.getValue();
                            var data = {};
                            data.url = '/fsxt/sys/department!saveDepartment.action';
                            data.params = params;
                            extAjaxSubmit.call(data);
                            window.hide();
                        }
                    },{
                        text:'取消',
                        handler:function(){
                            window.hide(Ext.get('createButton'));
                        }
                    }]
                });

                function extAjaxSubmit(){
                    Ext.MessageBox.wait("", "正在保存,请稍候...");
                    Ext.Ajax.request({
                        url:this.url,
                        method: 'post',
                        params: this.params,
                        success:function(response,options){
                            Ext.MessageBox.hide();
                            var result = Ext.JSON.decode(response.responseText);
                            if(result.success){
                                Ext.Msg.show({title:'提示',msg:'恭喜!保存数据成功!',icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK,
                                    fn:function(){
                                        store.loadPage(1);
                                    }
                                });
                            }else{
                                Ext.Msg.show({title:'提示',msg:result.msg,icon:Ext.MessageBox.WARNING,buttons:Ext.MessageBox.OK});
                            }
                        },
                        failure:function(response,options){
                            Ext.MessageBox.hide();
                            Ext.Msg.show({title:'提示',msg:'糟糕!保存失败.',buttons: Ext.MessageBox.OK,icon:Ext.MessageBox.ERROR});
                        }
                    });
                }

            });
        })();
    </script>
</head>
<body>
</body>
</html>